# coding:utf-8
from marshmallow import fields

from marshmallow.validate import Range

from common.im_util import IMMessageType
from ..extensions import ma


class IMMessageSchema(ma.Schema):
    """
    created by chenhj at 2017/11/22
    """
    id = fields.Integer(dump_only=True, dump_to='message_id')

    from_id = fields.Str(
        required=True,
        error_messages={
            'required': '缺少FROM_ID字段信息'
        }
    )
    to_id = fields.Str(
        required=True,
        error_messages={
            'required': '缺少TO_ID字段信息'
        }
    )
    msg_type = fields.Int(
        missing=IMMessageType.CHAT.value
    )
    message = fields.Str(
        required=True,
        error_messages={
            'required': '缺少MESSAGE字段信息'
        }
    )
    created_at = fields.Integer(
        required=True,
        error_messages={
            'required': '缺少CREATED_AT字段信息'
        }
    )


im_messages_schema = IMMessageSchema(many=True)


class IMCreateMessage(ma.Schema):
    """
    create by ld_wu 2017-12-06
    """

    message_list = fields.Nested(
        IMMessageSchema,
        many=True
    )


im_message_create_schema = IMCreateMessage()


class IMNotifySchema(ma.Schema):
    """
    create by ld_wu 2017-11-23
    消息通知
    """
    to_id = fields.String()
    from_id = fields.String()
    msg_type = fields.Integer()
    message = fields.String()
    un_read_count = fields.Integer()
    group_id = fields.String()
    created_at = fields.Integer()
    alias = fields.String()


im_notify_schema = IMNotifySchema(many=True)


class IMGroupSchema(ma.Schema):
    """
    create by ld_wu 2017-11-23
    消息group_id
    """

    group_id = fields.String(
        required=True,
        error_messages={
            'required': '缺少GROUP_ID字段信息'
        }
    )
    created_at = fields.Integer()

im_group_schema = IMGroupSchema()


class TimeLineMsgSchema(ma.Schema):

    to_ids = fields.List(
        fields.Integer(),
        required=True,
        error_messages={
            'required': '缺少to_ids字段信息'
        }
    )

time_line_msg_schema = TimeLineMsgSchema()


class TransferSchema(ma.Schema):

    to_id = fields.Integer(
        required=True,
        error_messages={
            'required': '缺少to_id字段信息'
        }
    )

    bid_point = fields.Integer(
        required=True,
        error_messages={
            'required': '缺少bid_point字段信息'
        },
        validate=[Range(1, error='汇币数量非法')]
    )
    remark = fields.String()

transfer_schema = TransferSchema()
